<?php

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;

class DefaultDb_Repositories_PointRepository extends EntityRepository
{

    public function getPointByNameOrAddress($stringSearch, $state)
    {
        $em = $this->getEntityManager();
        $dql = 'SELECT p
                FROM DefaultDb_Entities_Point p
                WHERE (p.name LIKE :search OR
                p.address LIKE :search )
                AND p.status = :status
                AND p.state = :state';
        $query = $em->createQuery($dql);
        $query->setParameter('search', '%' . $stringSearch . '%');
        $query->setParameter('status', DefaultDb_Entities_Point::STATUS_NORMAL);
        $query->setParameter('state', $state);
        $points = $query->getResult();
        return $points;
    }

    public function existByCode($code)
    {
        $em = $this->getEntityManager();
        $dql = 'SELECT p.id
                FROM DefaultDb_Entities_Point p
                WHERE p.code LIKE :code 
                AND p.status = :status';
        $query = $em->createQuery($dql);
        $query->setParameter('code', $code);
        $query->setParameter('status', DefaultDb_Entities_Point::STATUS_NORMAL);
        $points = $query->getResult();
        return count($points) > 0;
    }

    /**
     * Obtenemos los centros de intercambio activos e inactivos
     * @author Nylaye
     * @return array
     */
    public function getExchangesCenters()
    {
        return $this->findBy(array('type' => DefaultDb_Entities_Point::TYPE_EXCHANGE_CENTER));
    }

    /**
     * Obtenemos los centros de intercambio activos
     * @author Nylaye
     * @return array
     */
    public function getActiveExchangesCenters()
    {
        return $this->findBy(array('type' => DefaultDb_Entities_Point::TYPE_EXCHANGE_CENTER,
                    'status' => DefaultDb_Entities_Point::STATUS_NORMAL));
    }

    /**
     * Obtenemos los puntos de venta activos
     * @author Nylaye
     * @return array
     */
    public function getActiveSalePoints()
    {
        return $this->findBy(array('type' => DefaultDb_Entities_Point::TYPE_SALE_POINT,
                    'status' => DefaultDb_Entities_Point::STATUS_NORMAL));
    }

    /**
     * Insertamos un punto
     * @param string $code
     * @param string $name
     * @param integer $type
     * @param string $address
     * @param DefaultDb_Entities_State $state
     * @return DefaultDb_Entities_Point
     */
    public function insert($code, $name, $type, $address, DefaultDb_Entities_State $state)
    {
        $em = $this->getEntityManager();
        $newRecord = new DefaultDb_Entities_Point;
        $newRecord->setCode($code);
        $newRecord->setName($name); 
        $newRecord->setType($type);
        $newRecord->setAddress($address);
        $newRecord->setState($state);
        $newRecord->setStatus(DefaultDb_Entities_Point::STATUS_NORMAL);
        $em->persist($newRecord);
        $em->flush();
        return $newRecord;
    }
    
    /**
     * Insertamos un punto de venta
     * @param string $code
     * @param string $name
     * @param string $address
     * @param DefaultDb_Entities_State $state
     * @return DefaultDb_Entities_Point
     */
    public function insertSalePoint($code, $name, $address, DefaultDb_Entities_State $state)
    {
        return $this->insert($code, $name, DefaultDb_Entities_Point::TYPE_SALE_POINT, $address, $state);
    }
    
    /**
     * Insertamos un centro de intercambio
     * @param string $code
     * @param string $name
     * @param string $address
     * @param DefaultDb_Entities_State $state
     * @return DefaultDb_Entities_Point
     */
    public function insertExchangeCenter($code, $name, $address, DefaultDb_Entities_State $state)
    {
        return $this->insert($code, $name, DefaultDb_Entities_Point::TYPE_EXCHANGE_CENTER, $address, $state);
    }

}